/*
万能场景数据结构(采用先添加后编辑逻辑)

数据id：aid = 1，
问题：problem = '',
答案：answer = "",
会话ID：drawinglist_id = 2,
音频地址：audio_url = '',
gpt数据id: msg_id = '',
*/

import { ChatUtils, ChatRequestTest } from '@/common/gpt/chatUtils';
import { XCXChatUtils } from "@/common/gpt/xcxChatUtils.js";
import { minganciBiaoshu } from '@/common/gpt/type';
import { HOME_MODULE_TYPE } from '@/const';
// import { DIGITAL_HUMAN_TYPE } from "@/pages/tabBar/digitalHuman/types"

/** 万能场景逻辑 */
export const wannengMixin = {
	data() {
		return {
			wnSendMsgGen: null,
		}
	},
	onLoad() {
		this.wnSendMsgGen = this.createWnTiwenHandler();
	},
	methods: {
		/** 创建万能场景提问流程 */
		* createWnTiwenHandler() {
			yield this.wnWentiSave();
			yield this.wnStartTiwen();
			yield this.wnDaanSave();
		},
		/**
		 * 切换当前会话
		 * @property {String} id 会话ID
		 * */
		changeCSession({ id = "", title = "" }) {
			this.cSessionId = id;
			this.cSessionTitle = title;
			this.getMsgList();
			// if (this.isOpenEmptySessionRequest) {
			// 	this.isOpenEmptySessionRequest = false;
			// 	setTimeout(() => this.wnSendMsgGen.next(), 1)
			// }
		},
		/** 万能提问失败处理 */
		wnTiwenFailFn() {
			this.sendLoading = false;
			this.wnSendMsgGen = this.createWnTiwenHandler();
		},
		/** 发起提问 */
		async wnStartTiwen() {
			console.debug("wnStartTiwen__");
			var parentMessageId;
			if (this.fastTiwenCMsgId) parentMessageId = this.fastTiwenCMsgId;
			else if (this.msgList.length > 2)
				parentMessageId = this.msgList[this.msgList.length - 2]?.msg_id;
			this.fastTiwenCMsgId = "";
			const prompt = this.problem;
			this.problem = "";
			this.renderMsg = "";

			// #ifndef MP-WEIXIN
			this.gptProblem = prompt + "&&&" + (parentMessageId || "");
			console.debug("gptProblem____", this.gptProblem);
			// #endif

			// #ifdef MP-WEIXIN
			try {
				const res = await XCXChatUtils.request({
					options: { parentMessageId },
					prompt,
					type: Number(this.$store.state.cHomeModule == HOME_MODULE_TYPE.GPT4)
				});
				this.msgList[this.msgList.length - 1].msg_id = res.id;
				this.msgList[this.msgList.length - 1].answer = res.result;
				this.gptLastBackFn(res);
			} catch (e) { this.gptErrBackFn(e) }
			// #endif
		},
		/** 存储答案 */
		async wnDaanSave() {
			console.debug("wnDaanSave");
			try {
				const { aid, answer, msg_id, is_end, } = this.msgList[this.msgList.length - 1];
				console.debug(answer, this.msgList[this.msgList.length - 1])
				const res = await this.$post('6520b68e5566f', {
					record_id: aid,
					content: answer,
					msg_id: msg_id,
					is_end: is_end,
					send_type: this.cGptVersion,
				})
				await this.wnGetMsgList();
			} catch (e) {
				this.$msg((e || {}).msg || '存储答案失败', 'e');
				console.debug(e);
			}
			this.wnTiwenFailFn();
		},
		/** 存储问题 */
		async wnWentiSave() {
			try {
				const res = await this.$post('651530377997f', {
					content: this.problem,
					drawinglist_id: this.cSessionId,
					send_type: this.cGptVersion,
				});
				if (!this.msgList.length) this.setSeessionName({ aid: this.cSessionId, title: this.problem })
				this.resetGetUserInfo();
				await this.wnGetMsgList();
				setTimeout(() => { this.wnSendMsgGen.next(); }, 0)
			} catch (e) {
				this.$msg((e || {}).msg || '问题存储失败', 'e');
				this.wnTiwenFailFn();
			}
		},
		/** 停止提问 */
		wnStopTiwenFn() { ChatRequestTest.stopRequest() },
		/** 获取问答列表 */
		async wnGetMsgList() {
			console.debug("wnGetMsgList");
			try {
				const res = await this.$post('65a77110e9e75', {
					digitdrawinglist_id: this.cSessionId,
					page: this.page,
					// list_rows: 2,
					// send_type: this.$store.state.shuzirenVersion == DIGITAL_HUMAN_TYPE.BASE ? 1 : 2,
				});
				const _list = (res.data.data || []).map(item => {
					return {
						...item,
						// 自定义参数
						createAudioLoading: false, // 是否生成音频
						isPlayingAudio: false, // 是否播放当前音频
						answer: (item.answer || "").replace(/<br\/>/g, ""),
					}
				});
				var _num;
				if (this.page == 1) {
					_num = 1;
					this.msgList = _list;
					this.viewLastMsg(_num);
				} else {
					_num = this.msgList.length;
					this.msgList = [..._list, ...this.msgList];
					this.scrollToBeforeMore();
				}
				this.isHaveMore = res.data.total > this.msgList.length;
			} catch (e) { this.isHaveMore = false; }
			this.empty = !this.pageLoading && !this.msgList;
		},
		/** 设置会话标题 */
		async setSeessionName(info = {}) {
			await this.$post('6514eda9c2d0d', info);
			this.$refs.session.pageEditSessionTitle(info.title);
		},
		/** 会话标题被更新回调 */
		updateSessionTitlteSuccess(title = "") { /* this.cSessionTitle = title; */ }
	}
};
